// phpFTPUpload(localPath; remotePath; transferMode; host; user; password)
//
// Upload a file via FTP. Requires SmartPill PHP plugin.
//
// The file is always uploaded in PASSIVE mode. If you don't need this,
// comment out the call to ftp_pasv().
//
// trasnferMode should usually be 2 (FTP_BINARY); pass 1 for (FTP_ASCII).
//
// Returns:
//		 1..n		Successful copy (value indicates # of attempts to upload)
//		-1		localPath doesn't resolve to a valid file/path
//		-2		Couldn't log in to the FTP server
//		-3		Couldn't copy the file to the FTP server
//
Let (
	[
		$localPath = localPath ;
		$remotePath = remotePath ;
		$transferMode = transferMode ;
		$host = host ;
		$user = user ;
		$password = password ;

		phpCode = "error_reporting(0); //(E_ALL & ~E_NOTICE);¶
		¶
		$localPath = fm_evaluate('$localPath');¶
		$remotePath = fm_evaluate('$remotePath');¶
		$transferMode = fm_evaluate('$transferMode');¶
		$host = fm_evaluate('$host');¶
		$user = fm_evaluate('$user');¶
		$password = fm_evaluate('$password');¶
		¶
    // If you have timeouts trying to upload, increase $maxAttempts to a higher value.¶
    $maxAttempts = 1;¶
		¶
		$retVal = 0;¶
		if (!file_exists($localPath))¶
		{¶
			$retVal = -1;¶
		}¶
		else¶
		{¶
			$connection = ftp_connect($host);¶
			¶
			$login_result = @ftp_login($connection, $user, $password);¶
			if ((!$connection) || (!$login_result))¶
			{¶
				$retVal = -2;¶
			}¶
			else¶
			{¶
				ftp_pasv($connection, true);¶
				¶
				$didUpload = false;¶
				$numAttempts = 1;
				while (!$didUpload && ($numAttempts <= $maxAttempts))¶
				{¶
					$didUpload = ftp_put($connection, $remotePath, $localPath, $transferMode);¶
					if (!$didUpload)¶
					{¶
						$numAttempts++;¶
					}¶
				}¶
				if ($didUpload)¶
				{¶
					$retVal = $numAttempts;¶
				}¶
				else¶
				{¶
					$retVal = -3;¶
				}¶
				¶
				ftp_close($connection);¶
			}¶
		}¶
		echo $retVal;¶
	  "
	] ;
	PHP_Execute(phpCode)
)